package czk.concrent.enjoyModelWithNotLock.unsafe.assignToArray.practice;

import czk.concrent.enjoyModelWithNotLock.unsafe.UnsafeAccessor;
import sun.misc.Unsafe;

/**
 * 对int类型的数组进行赋值操作
 *
 * @Author:ChenZhangKun
 * @Date: 2021/7/7 17:06
 */
public class AssignToArrayPractice {
    public static void main(String[] args) {
        Unsafe unsafe = UnsafeAccessor.getUnsafe();
        // 创建数组
        int[] arr = new int[10];
        // 拿到偏移量
        int baseOffset = unsafe.arrayBaseOffset(arr.getClass());
        // 拿到每个数组的大小
        int scale = unsafe.arrayIndexScale(arr.getClass());
        //赋值
        long offset = baseOffset + scale * 2;
        unsafe.putOrderedInt(arr, offset, 10);
        System.out.println(arr[2]);
    }
}
